package com.dayuanit.dy17.atm.atmbackoffice.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.dayuanit.dy17.atm.atmbackoffice.service.ISalaryService;

import java.util.ArrayList;
import java.util.List;

public class SalaryDataListener extends AnalysisEventListener<SalaryData> {
    private static final int BATCH_COUNT = 2;

    List<SalaryData> salaryDataList = new ArrayList<SalaryData>();

    private ISalaryService salaryService;

    private long salaryId;

    public SalaryDataListener(ISalaryService salaryService, long salaryId) {
        this.salaryService = salaryService;
        this.salaryId = salaryId;
    }

    @Override
    public void invoke(SalaryData salaryData, AnalysisContext analysisContext) {
        salaryDataList.add(salaryData);
        if (salaryDataList.size() >= BATCH_COUNT) {
            //达到一定数量后，批量处理集合中的数据。
            processData();
            //处理成功后，将集合清空，迎接下一批数据。
            salaryDataList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        processData();
    }

    private void processData() {
        for (SalaryData salaryData : salaryDataList) {
            System.out.println(salaryData);
        }
        System.out.println("------------processData---------------");

        salaryService.saveSalaryDetail(salaryId, salaryDataList);
    }
}
